(function ($R) {

	/*	keys
		1301520467
		http://readable-static.tastefulwords.com/
		http://readable.tastefulwords.com/
	*/

	$R.path = 'http://nemowin32.googlecode.com/svn/trunk/sand1/readable/';
	$R.linksPath = 'http://readable.tastefulwords.com/';
	
	$R.win = window;
	$R.compileTime = '1301520467';
	if ($R.debug); else { $R.debug = false; }

	//	$R.path is esential
	//	I should probably have an error checking thing for it
	
	if ($R.debug)
	{
		/* debug stuff */
		/* =========== */

		$R.initializeWriteLogFunction = function ()
		{
			switch (true)
			{
				case (!(!($R.win.console && $R.win.console.log))):
					$R.writeLog = function (msg) { $R.win.console.log(msg); };
					break;
					
				case (!(!($R.win.opera && $R.win.opera.postError))):
					$R.writeLog = function (msg) { $R.win.opera.postError(msg); };
					break;
					
				default:
					$R.writeLog = function (msg) {};
					break;
			}
		};
	
		//	init write log
		$R.initializeWriteLogFunction();
		
		$R.log = function () {
			for (var i=0, il=arguments.length; i<il ; i++) {
				$R.writeLog(arguments[i]);
			}
		};

		//	timers
		
		$R.timers = [];
		
		$R.timerStart = function (timerName) {
			$R.timers.push({
				'name': timerName,
				'start': (new Date()).getTime()
			});
		};
		
		$R.timerEnd = function () {
			var 
				_t = $R.timers.pop(),
				_time = ((new Date()).getTime() - _t.start)
			;
			
			$R.log('TIMER / (' + _t.name + ') : ' + _time);
			return _time;
		};
	}
	else
	{
		/* non-debug */
		/* ========= */
		
		$R.writeLog 	= function () { return false; };
		$R.log 			= function () { return false; };
		$R.timerStart 	= function () { return false; };
		$R.timerEnd 	= function () { return false; };
	}

	
/* iframe html */
/* =========== */

	var 
		//	script name, URL pattern
		_custom_scripts =
		[
			['google-reader', 	/^http:\/\/www.google.com\/reader\/view\//i],
			['wikipedia', 		/^http:\/\/[a-z]+.wikipedia.org\/wiki\//i],
			['scribd', 			/^http:\/\/www.scribd.com\/doc\//i],
			['iframe', 			/^http:\/\/(ht|ow).ly\//i],
			['iframe', 			/^http:\/\/www.stumbleupon.com\/su\//i]
		],
		
		//	key, value
		_keys =
		{
			'css_url':		'https://dl.dropbox.com/u/13112361/readable/bulk-1301520467.css',
			'js_url':		'http://nemowin32.googlecode.com/svn/trunk/sand1/readable/bulk-1301520467.js',
			'jquery_url':	'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',
			'more_scripts': ''
		}
	;	

	//	ios?
	$R.iOS = (navigator.userAgent.match(/like Mac OS X/i) != null);
	
	//	embedded
	$R.embedded = ('embeddedOptions' in $R);
	
	//	embedded script
	if ($R.embedded && $R.embeddedOptions['script'] > '') {
		_keys['more_scripts'] += '<script type="text/javascript" src="'+$R.embeddedOptions['script']+'"></script>';
	}

	//	frame
	var _frameHTML = (''
		+	'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
		+		' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
		+	'<html id="html" xmlns="http://www.w3.org/1999/xhtml" class="'+($R.embedded ? 'embedded' : '')+'">'
		+	'<head>'
		+		'<link rel="stylesheet" href="[=css_url]" type="text/css" />'
		+		'<script type="text/javascript" src="[=jquery_url]"></script>'
		+		'[=more_scripts]'
		+		'<script type="text/javascript" src="[=js_url]"></script>'
		+	'</head>'
		+	'<body id="body"></body>'
		+	'</html>'
	)
	.replace(
		/\[=([a-z_]+?)\]/gi,
		function (_match, _key) {
			return _keys[_key];
		}
	);
	

/* the iframe + inline css */
/* ======================= */

	var _cssText = ''
		+	'#readable_app_iframe, #readable_app_cover { '
		+		'margin: 0; padding: 0; border: none; '
		+		'position: absolute; z-index: 999999999 !important; '
		+		'width: 100%; height: 100%; '
		+		'top: -100%; left: 0; '
		+	'} '
		+	'#readable_app_cover { ' 
		+							'z-index: 999999998 !important; '
		+		'margin: 0; padding: 0; border: 0; '
		+	'} '
	;
	
	var _cssElement = document.createElement('style');
		_cssElement.setAttribute('id', 'readableCSS1');
		_cssElement.setAttribute('type', 'text/css');
		
	if (_cssElement.styleSheet) { _cssElement.styleSheet.cssText = _cssText; }
		else { _cssElement.appendChild(document.createTextNode(_cssText)); }

	var _iframeElement = document.createElement('iframe');
		_iframeElement.setAttribute('id', 'readable_app_iframe');
		_iframeElement.setAttribute('frameBorder', '0');
		_iframeElement.setAttribute('allowTransparency', 'true');
		_iframeElement.setAttribute('scrolling', 'auto');
		_iframeElement.setAttribute('style', 'display: block !important;');
		
	var _body = document.getElementsByTagName('body')[0];
		_body.appendChild(_cssElement);
		_body.appendChild(_iframeElement);
	
	var _iframe = document.getElementById('readable_app_iframe'),
		_doc = (_iframe.contentDocument || _iframe.contentWindow.document);
		_doc.open();
		_doc.write(_frameHTML);
		_doc.close();
		
})(window.$readable);
